package com.epfresh.supplier.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.supplier.R;
import com.epfresh.supplier.adapter.PriceGroupAdapter;
import com.epfresh.supplier.entity.PriceGroup;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class PriceGroupActivity extends BaseActivity {

    ListView lv_deal_detail;
    PriceGroupAdapter bookYearGoodsAdapter;

    ArrayList<PriceGroup.PricesBean> list = new ArrayList<>();
    
    public String price, productId,productUnit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        mHttpLoad();
    }
    
    void initView(){
        setContentView(R.layout.activity_price_group);
        tvBack.setVisibility(View.VISIBLE);
        ivBack.setVisibility(View.VISIBLE);
        toolbarTitle.setText("分组销售价");

        lv_deal_detail = (ListView)findViewById(R.id.lv_deal_detail);
        bookYearGoodsAdapter = new PriceGroupAdapter(this,list);
        bookYearGoodsAdapter.setOnItemClickListener(this);
        lv_deal_detail.setAdapter(bookYearGoodsAdapter);

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("price",price);
        outState.putString("productId",productId);
        outState.putString("productUnit",productUnit);
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.ll_item:
                Object o = view.getTag(R.id.item_key_position);
                if (o != null && o instanceof Integer) {
                    int position = (int) o;
/*                    toast(""+position);*/
/*                    Intent detailIntent = new Intent(this,
                            BookGoodsMonthActivity.class);
                    detailIntent.putExtra("timeTitle", list.get(position).getName());
                    detailIntent.putExtra("type", list.get(position).getType());
                    detailIntent.putExtra("timeValue", list.get(position).getQueryDate());
                    detailIntent.putExtra("money", ""+list.get(position).getPrice()+"元");
                    detailIntent.putExtra("productId", list.get(position).getSpecId());
                    startActivity(detailIntent);*/
                }
                break;
        }
    }

    /**
     * 加载数据
     */
    private void mHttpLoad() {
        price = getIntent().getStringExtra("price");
        productId = getIntent().getStringExtra("productId");
        productUnit = getIntent().getStringExtra("productUnit");
        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();
        data.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_product_price4group);
        Map<String, String> map = new HashMap<>();
        map.put("price", price);//// TODO: 2017/9/29
        map.put("productId", productId);
        data.setParameters(map);
        request(data, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_product_price4group, onRequestListener);
    }

    private OnRequestListener<PriceGroup> onRequestListener = new OnRequestListener<PriceGroup>() {
        @Override
        public PriceGroup jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, PriceGroup.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            finish();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            finish();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<PriceGroup> response, Object tag) {
            hideProgressDialog();
            PriceGroup responseElement = response.getResponseElement();
            list.clear();
            list.addAll(responseElement.getPrices());
            bookYearGoodsAdapter.notifyDataSetChanged();
        }
    };

}
